package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.item.LoadingBatchRecordHWANE20782Item;
import com.enhe.gck.pojo.Batch;
import com.enhe.gck.pojo.LoadingBatchRecord;
import com.enhe.gck.service.impl.BatchService;
import com.enhe.gck.utils.LockUtil;
import com.kuangkie.carbon.common.RemoveRecordInfo;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.FGRecordComplexus;
import com.kuangkie.carbon.record.ProRecord;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Objects;

import static com.enhe.gck.CarbonEnumKeyValue.装车批次记录命令参数_过磅新增_gbxz;

@Repository(value = "hwane20782366042330866425862")
public class LoadingBatchRecordHWANE20782BNB implements SecondRoundImproveFuncGroup, RemoveFuncGroup {

    @Resource
    private BatchService batchService;

    @Override
    public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
                              ImproveResultFactory improveResultFactory) {
        // 过磅新增记录不执行下面逻辑!!!
        if (proRecord.isNew()
                && !Objects.equals(proRecord.getLong(LoadingBatchRecordHWANE20782Item.基本属性组_命令参数), 装车批次记录命令参数_过磅新增_gbxz)) {
            // 扣减响应批次编号
            LoadingBatchRecord loadingBatch = proRecord.getBean(LoadingBatchRecord.class);
            if (StrUtil.isBlank(loadingBatch.getBatchNumber())) {
                PublicMethod.returnMessage(improveResultFactory, "批次编号不能为空!");
                return;
            }
            // 获取批次信息
            Batch batch = batchService.getByName(loadingBatch.getBatchNumber());
            if (Objects.isNull(batch)) {
                PublicMethod.returnMessage(improveResultFactory, "批次信息不存在!");
                return;
            }
            if (LockUtil.containsKey(batch.getNumber())) {
                PublicMethod.returnMessage(improveResultFactory, "批次信息正在被修改,不能新增!请稍后重试!");
                return;
            }
            if (Objects.isNull(loadingBatch.getQuantity())) {
                PublicMethod.returnMessage(improveResultFactory, "装车数量不能为空!");
                return;
            }
            if (loadingBatch.getQuantity().compareTo(batch.getSumResidue()) > 0) {
                PublicMethod.returnMessage(improveResultFactory, "批次剩余量不足!");
                return;
            }
            LockUtil.setKey(batch.getNumber());
            try {
                // 扣减批号剩余量
                batch.setSumResidue(batch.getSumResidue().subtract(loadingBatch.getQuantity()));
                batchService.updateSumResidueBatch(Collections.singletonList(batch));
            } catch (Exception e) {
                PublicMethod.returnMessage(improveResultFactory, "扣减批次剩余量失败!");
            } finally {
                LockUtil.remove(batch.getNumber());
            }
        }
    }

    @Override
    public boolean beforeRemove(FuncGroupContext context, RemoveRecordInfo removedRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
        removedRecordInfo.getRecordCodes().forEach(id -> {
            LoadingBatchRecord batchRecord = CarbonPanel.getRecordDiscover(CarbonBaseConstant.装车批次记录_zcpcjl).getRecordBean(id, LoadingBatchRecord.class);
            if (Objects.isNull(batchRecord)) {
                return;
            }
            if (Objects.isNull(batchRecord.getQuantity())) {
                return;
            }
            // 获取批次信息
            Batch batch = batchService.getByName(batchRecord.getBatchNumber());
            if (Objects.isNull(batch)) {
                return;
            }
            if (LockUtil.containsKey(batch.getNumber())) {
                PublicMethod.returnMessage(defDeleteMessageFactory, "批次信息正在被修改,不能删除!请稍后重试!");
                return;
            }
            LockUtil.setKey(batch.getNumber());
            try {
                // 扣减批号剩余量
                batch.setSumResidue(batch.getSumResidue().add(batchRecord.getQuantity()));
                batchService.updateSumResidueBatch(Collections.singletonList(batch));
            } catch (Exception e) {
                PublicMethod.returnMessage(defDeleteMessageFactory, "删除订单批次失败!");
            } finally {
                LockUtil.remove(batch.getNumber());
            }
        });
        return true;
    }
}
